<cfcomponent output="false">
	
	<!--- --->

	<cffunction name="listPlayers" access="public" output="false" returntype="query">
		<cfargument name="userID" required="true" />

		<cfset var local = {} />
		<cfif arguments.userID neq '' and arguments.userID neq '-1'>
			<cfset arguments.userID = arguments.userID />
		</cfif>
		
		<cfquery name="local.listPlayers" datasource="fundrdb">
			select total_rating / denominator as rating,list.* from (
				select 
				user.id,
				first_name,
				middle_name,
				last_name,
				email,
				password,
				my_rating.rating as my_rating,
				sum(ifNull(pong_rate_user.rating,0)) as total_rating,
				count(case when pong_rate_user.rating is null then 0 else 1 end) as denominator
				from user
				left join pong_rate_user on pong_rate_user.player_id = user.id
				left join pong_rate_user my_rating on my_rating.player_id = user.id
					and my_rating.user_id = #application.q.queryID(arguments.userID)#
	     		where user.active = 1
				group by user.id
			) list
			order by total_rating / denominator desc, first_name desc
		</cfquery>

		<cfreturn local.listPlayers />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getRules" access="public" output="false" returntype="query">
		<cfargument name="userID" required="true" />
		
		<cfif arguments.userID eq "" or arguments.userID eq -1>
			<cfset arguments.userID = 0 />
		</cfif>
		
		<cfquery name="local.getRules" datasource="fundrdb">
			select
			pong_rule.id as pong_rule_id,
			pong_rule.name as pong_rule,
			pong_rule_option.description as pong_rule_option,
			pong_rule_option.id as pong_rule_option_id,
			pong_vote.pong_rule_option_id as vote,
			ifNull(vote_count.counts,0) as counts
			from pong_rule
			inner join pong_rule_option on pong_rule_option.pong_rule_id = pong_rule.id
			and pong_rule_option.active = 1
			left join (
				select pong_rule_option_id,			
				count(pong_vote.id) as counts
				from pong_vote
				group by pong_rule_option_id
			) vote_count on vote_count.pong_rule_option_id = pong_rule_option.id
			left join pong_vote on pong_vote.pong_rule_id = pong_rule.id
				and pong_vote.user_Id = #application.q.queryID(arguments.userID)#
			order by pong_rule.id asc, pong_rule_option.id asc
		</cfquery>

		<cfreturn local.getRules />
		
	</cffunction>
	
	<!--- --->
	
</cfcomponent>